package com.person.title.action;

import java.util.List;

import com.person.commons.struts2.BaseAction;
import com.person.title.model.Title;
import com.person.title.model.TitleClass;
import com.person.title.model.TitleFunc;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleClassService;
import com.person.title.service.TitleFuncService;

public class TitleFuncAction extends BaseAction {

	private TitleFuncService titleFuncService ;
	private TitleClassService titleClassService ;
	private List <TitleFunc> titleFuncs ;
	private TitleClass titleClass ;
	private TitleFunc titleFunc ;
	private String classCode ;
	private String funcCode ;
	private String classCodeGroup ;
	private String className ;
	private int flag = 0 ;

	
	/**
	 * 进入添加子功能
	 * 
	 * @return
	 */
	public String preCreate () {
		
		String forward = "" ;
		
		if ((!"".equals(titleFunc.getFuncId()) && (titleFunc.getFuncId()!=null))) {
			titleFunc = this.titleFuncService.findTitleFuncById(titleFunc.getFuncId()) ;
			classCode = titleFunc.getClassCodeGroup() ;
			className = titleFunc.getFuncName() ;	
		} else {
			titleClass = titleClassService.findSysClassId(titleClass.getClassId()) ;
			classCode = titleClass.getClassCode() ;
			className = titleClass.getClassName() ;
		}
		
		if (classCode.length()<=8) {
			forward = "createFunc" ;			
		} else {
			info = getText ("func.create.fail") ;
			forward = "create_fail" ;
		}
		
		return forward ;		

	}
	
	
	/**
	 * 添加子功能
	 * 
	 * @return
	 */
	public String create () {
				
		String forward = "" ;
		String classCodeGroup = classCode + funcCode ;
		titleFunc.setClassCodeGroup(classCodeGroup) ;		
		char state = this.titleFuncService.createTitleFunc(titleFunc) ;		
		
		if(state == '1'){
			info = getText("func.create.success");	
			flag = 1 ;
			forward = "create_success";
		}else if(state == '2'){
			
			info = getText("func.code");
			forward = INPUT;
			
		}else if(state == '3'){
			
			info =  getText("func.name");
			forward = INPUT;
		}
		
		return forward;
	}
	
	
	/**
	 * 进入修改子功能
	 * 
	 * @return
	 */
	public String preModify () {
				
		titleFunc = this.titleFuncService.findTitleFuncById(titleFunc.getFuncId()) ; 
		
		classCodeGroup = titleFunc.getClassCodeGroup() ;
		
		funcCode = classCodeGroup.substring(classCodeGroup.length()-2, classCodeGroup.length()) ;
		classCode = classCodeGroup.substring(0, classCodeGroup.length()-2) ;
		
		if (classCode.length()==2) {
			TitleClass titleClass1 = this.titleClassService.findTitleClassByCode(classCode) ;
			className = titleClass1.getClassName() ;
			
		} else {		
			TitleFunc titleFunc1 = this.titleFuncService.findTitleFuncByCode(classCode) ;
			className = titleFunc1.getFuncName() ;
		}
		
		return "modifyFunc" ;
	}
	
	
	/**
	 * 修改子功能节点
	 * 
	 * @return
	 */
	public String modify () {
		
		String forward = "" ;
		
		String classCodeGroup1 = classCode + funcCode ;
		titleFunc.setClassCodeGroup(classCodeGroup1) ;
				
		char state = this.titleFuncService.modifyTitleFunc(titleFunc) ;
		char state1 = this.titleFuncService.modifyJuniorTitleFunc(titleFunc, classCodeGroup) ;
		
		if(state == '1' && state1=='1'){
			info = getText("func.modify.success");	
			flag = 1 ;
			forward = "modify_success";
		}else if(state == '2'){
			
			info = getText("func.code");
			forward = INPUT;
			
		}
		
		return forward ;
	}
	
	
	/**
	 * 删除子功能节点
	 * 
	 * @return
	 */
	public String delete () {
		String forward = "" ;
		
		if(titleFunc != null) {
			titleFunc = this.titleFuncService.findTitleFuncById(titleFunc.getFuncId()) ;
			char state = this.titleFuncService.deleteTitleFunc(titleFunc) ;
			if(state == '1') {
				info = getText("func.delete.success") ;
				flag = 1;
				forward = "delete_success" ;
			}else if(state == '0') {
				
				info = getText("func.delete.fail");
				forward = "delete_success";				
			}
		}
		return forward;
	}
	
	
	/**
	 * 管理子功能节点
	 * 
	 * @return
	 */
	public String manageFunc () {
				
		titleFunc = this.titleFuncService.findTitleFuncById(titleFunc.getFuncId()) ;
		
		titleFuncs = this.titleFuncService.juniorTitleFuncList(titleFunc.getFuncId()) ;
		
		classCode = titleFunc.getClassCodeGroup() ;
		
		className = className + "→" + titleFunc.getFuncName() ;
		
		return "manageFunc" ;
	}
	
	
	
	
	public TitleFuncService getTitleFuncService() {
		return titleFuncService;
	}

	public void setTitleFuncService(TitleFuncService titleFuncService) {
		this.titleFuncService = titleFuncService;
	}

	public TitleClassService getTitleClassService() {
		return titleClassService;
	}

	public void setTitleClassService(TitleClassService titleClassService) {
		this.titleClassService = titleClassService;
	}

	public List<TitleFunc> getTitleFuncs() {
		return titleFuncs;
	}

	public void setTitleFuncs(List<TitleFunc> titleFuncs) {
		this.titleFuncs = titleFuncs;
	}

	public TitleClass getTitleClass() {
		return titleClass;
	}

	public void setTitleClass(TitleClass titleClass) {
		this.titleClass = titleClass;
	}

	public TitleFunc getTitleFunc() {
		return titleFunc;
	}

	public void setTitleFunc(TitleFunc titleFunc) {
		this.titleFunc = titleFunc;
	}

	public String getClassCode() {
		return classCode;
	}

	public void setClassCode(String classCode) {
		this.classCode = classCode;
	}

	public String getFuncCode() {
		return funcCode;
	}

	public void setFuncCode(String funcCode) {
		this.funcCode = funcCode;
	}

	public String getClassCodeGroup() {
		return classCodeGroup;
	}

	public void setClassCodeGroup(String classCodeGroup) {
		this.classCodeGroup = classCodeGroup;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}


	public String getClassName() {
		return className;
	}


	public void setClassName(String className) {
		this.className = className;
	}
	
	
	
	
	
	
	
	
	
	
	
}
